package org.example;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Client {
    private  static String PORT ;
    private  static String ADDRESS;
    private static String IP;

    public static void main(String[] args) throws IOException {
        //读取properties
        InputStream inputStream = Client.class.getClassLoader().getResourceAsStream("client.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        //获取端口和地址
        System.out.println("port=" + properties.getProperty("port"));
        System.out.println("address=" + properties.getProperty("address"));
        System.out.println("ip=" + properties.getProperty("ip"));
        PORT = properties.getProperty("port");
        ADDRESS = properties.getProperty("address");
        IP = properties.getProperty("ip");
        inputStream .close();
        //调用多线程
        scheduled();
    }
    public static void scheduled(){
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
        Runnable task = () -> {
            final File folder = new File(ADDRESS);
            readFile(folder);
        };

        // 这里设置的是固定的1秒钟延时,每隔3秒执行一次
        executor.scheduleAtFixedRate(task, 1000, 3000, TimeUnit.MILLISECONDS);
    }
  /*  public static void scheduled() {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
        // 每隔几秒执行一次操作
        int intervalInSeconds = 3;
        executor.scheduleAtFixedRate(new MyTask(), 0, intervalInSeconds, TimeUnit.SECONDS);
        // 等待一段时间后关闭执行器
        int durationInSeconds = 10;
        executor.schedule(() -> {
            executor.shutdown();
        }, durationInSeconds, TimeUnit.SECONDS);
    }

    static class MyTask implements Runnable {

        @Override
        public void run() {
            // 执行相应的操作
            //读取文件夹下的内容
            final File folder = new File(ADDRESS);
            final Fileread fileread = new Fileread();
            readFile(folder);

        }
    }*/
    //读取文件
    public static void readFile(File folder){
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    readFile(file);
                }
            }
        } else {
            //执行相关操作
            System.out.println("文件名：" + folder.getName());

        }
    }
}
